//
//  String+ZLExtensions.swift
//  nf-ios
//
//  Created by itzhaolei on 2021/9/1.
//

public extension String {
    
    func URLEncoding() -> String {
        return (self as NSString).addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) ?? ""
    }
    
    enum Target {
        case size, width, height
    }
    /// 计算字符串尺寸值
    /// - Parameters:
    ///   - target: 要获取的目标，该目标一定要Returns返回的使用价值相对应
    ///   - limit: 限制条件：例如想要获取宽的值，就填写对高的限制，依次类推
    /// - Returns: 处理下文
    func getSize(font: UIFont?, target: Target, limit: CGFloat = CGFloat(MAXFLOAT)) -> CGSize {
        guard let safeFont = font else { return .zero }
        var limitSize: CGSize = .zero
        if target == .width {
            limitSize = .init(width: CGFloat(MAXFLOAT), height: limit)
        }else if (target == .height) {
            limitSize = .init(width: limit, height: CGFloat(MAXFLOAT))
        }else {
            limitSize = .init(width: CGFloat(MAXFLOAT), height: CGFloat(MAXFLOAT))
        }
        let size = (self as NSString).boundingRect(with: limitSize, options: .usesLineFragmentOrigin, attributes: [.font : safeFont], context: nil).size
        if target == .width {
            return .init(width: size.width, height: limit)
        }else if (target == .height) {
            return .init(width: limit, height: size.height)
        }else {
            return size
        }
    }
    
}
